package com.iflytek.autofly.setting.application;

import android.app.Activity;
import android.app.Application;
import android.graphics.Typeface;
import android.os.SystemProperties;

import com.iflytek.autofly.setting.biz.Notice;
import com.iflytek.autofly.utils.FlyCrashHandler;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;

import java.util.LinkedList;
import java.util.List;

/**
 * <b>Description: </b>MainApplication<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年5月26日</td>
 * <td>上午9:19:25</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class MainApplication extends Application {

    /*
     * 是否发布标识,正式发布时修改此处可实现 1.关闭Logs中的日志输出 2.打开全局的异常捕获,处理程序crash
     */
    public static boolean publishFlag = false;
    // 添加注释
    public static MainApplication context;
    // 添加注释2
    public static Typeface TEXT_TYPE;
    private static MainApplication mApplication;
    private final String TAG = "MainApplication";

    // ************* 全局变量，注销可不销毁*****************//
    public int screenWidth;
    public int screenHeight;
    private List<Activity> activityList = new LinkedList<Activity>();

    public MainApplication() {
        mApplication = this;
    }

    public static MainApplication instance() {
        return mApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        FlyCrashHandler.getInstance().init(this);

        context = this;
        /*
         * // 后台接口服务 Intent commandServiceIntent = new Intent(this,
		 * CommandService.class); startService(commandServiceIntent);
		 * 
		 * if(publishFlag){ //处理异常 CrashHandler crashHandler =
		 * CrashHandler.getInstance();
		 * crashHandler.init(getApplicationContext()); }
		 */

        // loadTTF();
        Notice.getInstatnce().init(getApplicationContext());

        FlowerCollector.updateOnlineConfig(context, null);
        FlowerCollector.setParameter("duid", SystemProperties.get("persist.sys.navi.did"));
        FlowerCollector.setParameter("url_param_onlog", "catalog=auto"); //此处固定写死，不要做任何修改
    }

    /**
     * loadTTF:(加载自定义字体). <br/>
     */
    private void loadTTF() {
        try {
            TEXT_TYPE = Typeface.createFromAsset(getAssets(),
                    "fonts/helvetica_lt_25_ultra_light.ttf");
        } catch (Exception e) {
            TEXT_TYPE = null;
            FlyLog.d(TAG, "加载字体失败");
        }
    }

    /**
     * addActivity:添加Activity到容器中 <br/>
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public int getActivityList() {
        return activityList.size();
    }

    /**
     * finishActivities:遍历所有Activity并finish. <br/>
     */
    public void finishActivities() {
        for (Activity activity : activityList) {
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }

}
